<?php
//模块名_控制名
class Product_PackageController extends Zend_Controller_Action
{

	public function preDispatch(){
		$this->tplDirectory = "product/package/";	//控制对应模板目录
	}
	
	public function indexAction(){
		$currencyArr = Common_Service_Currency::getAll();
		$this->view->currencyArr = $currencyArr;
		echo Cff::renderTpl($this->tplDirectory."index.tpl","layout");	
    }
    
    public function listAction(){
    	$pp_name = $this->_request->getParam('pp_name','');
		$status = $this->_request->getParam('status','');
		
    	$cond =array();
    	if(!empty($pp_name)){    		
    		$cond['pp_name'] = $pp_name;
    	}
    	if(isset($status) && $status !==''){
    		$cond['status'] = $status;
    	}
    	$data=array('ask'=>0);
    	$page = $this->_request->getParam('page',1);
    	$pageSize = $this->_request->getParam('pageSize');		
    	$orderby = 'pp_id';
		$result = Product_Service_Package::getByCond($cond,'*',$orderby, $pageSize, ($page - 1)*$pageSize);		
		$total = Product_Service_Package::getByCond($cond,'count(*)',$orderby, 0, 0);
		if($total>0){
			$data=array(
				'total'=>$total,
				'result'=>$result,
				'ask'=>1
			);
		}
		die(Zend_Json::encode($data));
    }
    
    public function editAction(){
    	$pp_id = $this->_request->getParam('pp_id','');
    	$pp_name = $this->_request->getParam('pp_name','');
    	$warehouse_id = $this->_request->getParam('warehouse_id','');
    	$pp_length = $this->_request->getParam('pp_length','');
    	$pp_width = $this->_request->getParam('pp_width','');
    	
    	$pp_height = $this->_request->getParam('pp_height','');
    	$pp_weight = $this->_request->getParam('pp_weight','');
    	$pp_price = $this->_request->getParam('pp_price','');
    	$currency_code = $this->_request->getParam('currency_code','');
    	$status = (int) $this->_request->getParam('status',1);
    	$customer_id = trim($this->_request->getParam('customer_id',''));
    	
    	$row = array(
			'pp_name' => $pp_name,
			'warehouse_id' => $warehouse_id,
			'customer_id' => $customer_id,
			'status' => $status,
			'pp_length' => $pp_length,
			'pp_width' => $pp_width,
			
			'pp_height' => $pp_height,
			'pp_weight' => $pp_weight,
			'pp_price' => $pp_price,
			'currency_code' => $currency_code,
			
		);
		$data = $resArr = array();
		if($pp_id){
			unset($row['pp_name']);
			$row['pp_last_update'] = date('Y-m-d H:i:s');
			$resArr = Product_Service_Package::update($row,$pp_id);
		}else {
			$row['pp_add_time'] = date('Y-m-d H:i:s');
			$resArr = Product_Service_Package::add($row);
		}
		if($resArr){
    		$data = $resArr;		
		}
		die(Zend_Json::encode($data));	
    }
    
    public function deleteAction(){
    	$id = $this->_request->getParam('id','');
    	$data = array('ask'=>0);
    	$resArr = array();
    	if(!empty($id)){
    		$resArr = Product_Service_Package::delete($id);
    		if($resArr){
    			$data = $resArr;		
			}
    	}
		die(Zend_Json::encode($data));
    }
    
    public function singleAction(){
    	$pp_id = $this->_request->getParam('id','');
    	$data=array('ask'=>0);
    	if($pp_id){
    		$result = Product_Service_Package::getByPpid($pp_id);
    		if( count($result) ){
    			$data['result'] = $result;
    			$data['ask'] = 1;			
			}
    	}
    	die(Zend_Json::encode($data));
    }

    //获取对应客户包材
    public function getAvailableAction(){
    	$warehouseId= trim($this->_request->getParam('warehouse_id','1'));
    	$customerId = trim($this->_request->getParam('customer_id',''));
        $result=array('ask'=>0,'result'=>array());
        if($warehouseId=='' || $customerId=='' ){
            die(Zend_Json::encode($result));
        }
    	$package = Product_Service_Package::getAllAvailable($warehouseId,$customerId);
        if(!empty($package)){
          $result=array(
              'ask'=>1,
              'result'=>$package
          );
        }
        die(Zend_Json::encode($result));
    }
    
}